package cn.com.huzo.repository.purchase.impl;

import cn.com.huzo.model.entities.purchase.PoNoticeEntity;
import cn.com.huzo.repository.purchase.PoNoticeRepository;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Repository;

/**
 * 采购下单通知数据访问对象。
 *
 * @author 吴旭
 */
@Repository("PoNoticeRepository")
public class PoNoticeRepositoryImpl extends GenericRepositoryImpl<PoNoticeEntity, String>
        implements PoNoticeRepository {

    private final String namespace = "cn.com.huzo.repository.purchase.PoNoticeRepository";

    /**
     * 构造函数,设置the instance to ProductRepositoryImpl.class.
     */
    public PoNoticeRepositoryImpl() {
        super(PoNoticeEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchForCount(Map<String, Object> criterion) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForCount"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<PoNoticeEntity> fetchForPage(Map<String, Object> criterion) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForPage"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> fetchPurchasedByName(Long mrpId, Short mrpType) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("MrpId", mrpId);

        switch (mrpType) {
            case 0:
                return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "fetchPurchasedByNameByMrpId"), criterion);
            case 1:
                return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "fetchPurchasedByNameByMrpFeedingId"), criterion);
            default:
                return new ArrayList();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchNewNoticeByRecipient(Long purchasedBy) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("PurchasedBy", purchasedBy);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchNewNoticeByRecipient"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer setReceiptedFlag(String poNoticeId, Short mrpType) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("PoNoticeId", poNoticeId);

        switch (mrpType) {
            case 0:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setReceiptedFlag01"), criterion);
            case 1:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setReceiptedFlag01"), criterion);
            case 2:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setReceiptedFlag01"), criterion);
            case 3:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setReceiptedFlag02"), criterion);
            default:
                return 0;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer setOverFlag(String poNoticeId, Short mrpType, String overFlag) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("PoNoticeId", poNoticeId);
        criterion.put("OverFlag", overFlag);

        switch (mrpType) {
            case 0:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setOverFlag01"), criterion);
            case 1:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setOverFlag01"), criterion);
            case 2:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setOverFlag01"), criterion);
            case 3:
                return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "setOverFlag02"), criterion);
            default:
                return 0;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer sendPoNotice(Map<String, Object> criterion) {
        switch (MapUtils.getShort(criterion, "BillType")) {
            case 0:
                return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "sendPoNotice01"), criterion);
            case 1:
                return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                        namespace, "sendPoNotice02"), criterion);
            default:
                return 0;
        }
    }
}
